package book.action;

import java.util.List;

import book.dao.ShowDao;
import book.dao.Impl.ShowDaoImpl;
import book.entity.Book;

public class ShowAction extends BaseAction {
	private int currentPage;
	private String pageIndex;
	private int totalPages;
	
	public String execute(){
		String pageIndex=getRequest().getParameter("pageIndex");
		
		if(pageIndex==null){
			pageIndex="1";
		}
		int currentPage=Integer.parseInt(pageIndex);
		int pageSize=3;
		ShowDao showDao=new ShowDaoImpl();
		int count=showDao.countAllBooks();
		int totalPages=count%pageSize==0?count/pageSize:count/pageSize+1;
		if(currentPage<1){
			currentPage=1;
		}else if(currentPage>totalPages){
			currentPage=totalPages;
		}
		int pageFirst=pageSize*(currentPage-1);
		List<Book> list=showDao.showAllBooks(pageFirst, 3);
		if(list!=null){
			getRequest().setAttribute("list", list);
			getRequest().setAttribute("currentPage", currentPage);
			getRequest().setAttribute("totalPages", totalPages);
			getRequest().setAttribute("count", count);
			return SUCCESS;
		}else{
			System.out.println("list为空");
			return ERROR;
		}
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public String getPageIndex() {
		return pageIndex;
	}
	public void setPageIndex(String pageIndex) {
		this.pageIndex = pageIndex;
	}
	public int getTotalPages() {
		return totalPages;
	}
	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}
	
	
}
